package com.tsj.spzx.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tsj.spzx.mapper.SysRoleMapper;
import com.tsj.spzx.mapper.SysRoleUserMapper;
import com.tsj.spzx.model.dto.system.SysRoleDto;
import com.tsj.spzx.model.entity.system.SysRole;
import com.tsj.spzx.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Override
    public PageInfo<SysRole> findByPage(Integer current, Integer limit, SysRoleDto sysRoleDto) {
        // 开启分页查询
        PageHelper.startPage(current, limit);
        // 开始查询
        List<SysRole> list = sysRoleMapper.findByPage(sysRoleDto);
        PageInfo<SysRole> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void saveSysRole(SysRole sysRole) {
        sysRoleMapper.saveSysRole(sysRole);
    }

    @Override
    public void updateSysRole(SysRole sysRole) {
        sysRoleMapper.updateSysRole(sysRole);
    }

    @Override
    public void deleteSysRole(Long id) {
        sysRoleMapper.deleteSysRole(id);
    }

    @Override
    public Map<String, Object> findAllRoles(Long userId) {
        // 1、先查询所有的角色信息
        List<SysRole> roleList = sysRoleMapper.findAllRoles();

        // 2、再查询用户已经拥有的角色信息
        List<Long> userRoleList = sysRoleUserMapper.findUserRoleByUserId(userId);

        Map<String, Object> map = new HashMap<>();
        map.put("allRolesList", roleList);
        map.put("sysUserRoles", userRoleList);
        return map;
    }
}
